<?php


namespace app\api\controller;

use app\api\model\Goods as GoodsModel;

class Pwd
{
    //口令加密
    public function goodsPasswd(){
        //        接受商品id
        $goods_id=input('goods_id');
        //接受用户id
        $uid=input('uid');
        //判断用户id
        if(!is_numeric($uid)){
            return json(['code'=>400,'msg'=>'参数不正确','uid'=>null]);
        }
        //判断商品id
        if(!is_numeric($goods_id)){
            return json(['code'=>400,'msg'=>'参数不正确','goods_id'=>null]);
        }

       $goodsRest= GoodsModel::where('good_id',$goods_id)->find()->toArray();
      //商品名称
        $goods_name=$goodsRest['good_name'];
            //加密
        $dataRest=encrypt($goods_name,$uid);

        if($dataRest){
            return json(['code'=>200,'msg'=>'生成成功','pwd'=>$dataRest]);
        }

    }

    //口令解密
    public function decryptGoods(){
        //        接受商品id
        $goods_id=input('goods_id');
        //接受用户id
        $uid=input('uid');
            //接受加密字符串
        $goods_name=input('good_name');


        //判断用户id
        if(!is_numeric($uid)){
            return json(['code'=>400,'msg'=>'参数不正确','uid'=>null]);
        }
        //判断商品id
        if(!is_numeric($goods_id)){
            return json(['code'=>400,'msg'=>'参数不正确','goods_id'=>null]);
        }

        $goodsRest= GoodsModel::where('good_id',$goods_id)->find()->toArray();
        //商品名称

        //加密
        $dataRest=decrypt($goods_name,$uid);
        
        //返回三要素
        if($dataRest){
            return json(['code'=>200,'msg'=>'解密成功','pwd'=>$dataRest]);
        }

    }
}
